﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GIMS.MODEL;
using KPIS.GERP.GIMS.IDAL;

namespace KPIS.GERP.GIMS.DAL
{
    public sealed class SysRecordStatusDAL : IDALSysRecordStatus
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public SysRecordStatusDAL()
        {

        }

        public IList<SysRecordStatusInfo> GetSysRecordStatus()
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<SysRecordStatusInfo> infoList = new List<SysRecordStatusInfo>();
            try
            {
                dbManager.Open();

                strSQL = "SELECT *"
                        + " FROM SYS_RECORD_STATUS";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        SysRecordStatusInfo info = new SysRecordStatusInfo();
                        info.Status = (dr[i]["RECORD_STATUS"]).ToString();
                        info.Name = (dr[i]["RECORD_STATUS_NAME"]).ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<SysRecordStatusInfo> GetSysRecordStatusTransaction()
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<SysRecordStatusInfo> infoList = new List<SysRecordStatusInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@work_status", SystemInfo._WorkRecordStatus);
                dbManager.AddParameters(1, "@process_status", SystemInfo._ProcessRecordStatus);
                dbManager.AddParameters(2, "@approve_status", SystemInfo._ApproveRecordStatus);

                strSQL = "SELECT *"
                        + " FROM SYS_RECORD_STATUS"
                        + " WHERE RECORD_STATUS = @work_status"
                            + " OR RECORD_STATUS = @process_status"
                            + " OR RECORD_STATUS = @approve_status";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        SysRecordStatusInfo info = new SysRecordStatusInfo();
                        info.Status = (dr[i]["RECORD_STATUS"]).ToString();
                        info.Name = (dr[i]["RECORD_STATUS_NAME"]).ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
    }
}
